package com.xy.dyson.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xy.dyson.entity.Resources;
import com.xy.dyson.entity.SyntheticRelation;
import com.xy.dyson.service.ResourcesService;
import com.xy.dyson.service.SyntheticRelationService;
import com.xy.dyson.utils.R;
import com.xy.dyson.vo.RelationVo;
import com.xy.dyson.vo.SaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Resources)表控制层
 *
 * @author XY
 * @since 2021-01-28 16:11:31
 */
@Controller
@RequestMapping("resources")
public class ResourcesController {
    /**
     * 服务对象
     */
    @Resource
    private ResourcesService resourcesService;

    @Autowired
    SyntheticRelationService syntheticRelationService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ResponseBody
    public Resources selectOne(Integer id) {
        return this.resourcesService.queryById(id);
    }

    @GetMapping("selectTree")
    @ResponseBody
    public List<Resources> selectTree(Integer id, HttpServletResponse response){
        Resources res= resourcesService.queryTree(id);
        List<Resources> list =new ArrayList<>();
        list.add(res);
        return list;
    }

    @GetMapping("/toAdd")
    public String toAdd(Model model){
        Resources resources = new Resources();
        List<Resources> list= resourcesService.queryAll(resources);
        model.addAttribute("data",list);
        return "add";
    }

    @GetMapping("/toUpdate")
    public String toUpdate(Model model){
        Resources resources = new Resources();
        List<Resources> list= resourcesService.queryAll(resources);
        model.addAttribute("data",list);
        return "update";
    }

    @GetMapping("/toList")
    public String toList(Model model){
        Resources resources = new Resources();
        List<Resources> list= resourcesService.queryAll(resources);
        model.addAttribute("data",list);
        return "list";
    }


    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody SaveVo saveVo){
        try{
            Resources resources = new Resources();
            resources.setTitle(saveVo.getTitle());
                List<Resources> resources1 = resourcesService.queryAll(resources);
            if(resources1!=null&&resources1.size()>0){
                throw new RuntimeException("该物品已存在");
            }
            Resources insert = resourcesService.insert(resources);
            List<RelationVo> relationVos = saveVo.getRelationVos();
            List<SyntheticRelation> collect = relationVos.stream().map(item -> {
                SyntheticRelation syntheticRelation = new SyntheticRelation();
                syntheticRelation.setResourceId(insert.getId());
                syntheticRelation.setRequireResourceId(item.getRequireResourceId());
                return syntheticRelation;
            }).collect(Collectors.toList());
            syntheticRelationService.insertBatch(collect);
            return R.ok();
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/update")
    @ResponseBody
    @CacheEvict(value = "dyson",allEntries = true)
    public R update(Integer resourceId,@RequestBody List<RelationVo> relationVos){

        try{
            //先删除之前的内容
            syntheticRelationService.deleteByResourceId(resourceId);
            List<SyntheticRelation> collect = relationVos.stream().map(item -> {
                SyntheticRelation syntheticRelation = new SyntheticRelation();
                syntheticRelation.setResourceId(resourceId);
                syntheticRelation.setRequireResourceId(item.getRequireResourceId());
                return syntheticRelation;
            }).collect(Collectors.toList());
            if(collect.size()>0){
                syntheticRelationService.insertBatch(collect);
            }
            return R.ok();
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }


}